/*
 * Created by JFormDesigner on Thu Dec 01 13:23:09 EET 2011
 */

package tr.gov.tubitak.bilgem.esya.certviewer;

import tr.gov.tubitak.bilgem.esya.certviewer.viwerasn.ECertField;

import javax.swing.*;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 * @author Haydar Karabas
 */
public class ECertFieldTable extends JTable {
    public ECertFieldTable() {
        initComponents();
    }

    ECertFieldTypeFilter certFieldTypeFilter;
    @Override
    public void setModel(TableModel dataModel) {
        super.setModel(dataModel);    //To change body of overridden methods use File | Settings | File Templates.
        TableColumnModel columnModel1 = getColumnModel();
        if(columnModel.getColumnCount()>0)
        {
            getColumnModel().getColumn(0).setCellRenderer(new ECertFieldTableRenderer());
        }

        TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(dataModel);
        certFieldTypeFilter = new ECertFieldTypeFilter();
        sorter.setRowFilter(certFieldTypeFilter);
        setRowSorter(sorter);
        getSelectionModel().setSelectionInterval(0,0);
    }

    public void setFilterFieldType(ECertField.ECertFieldType filterFieldType) {
            this.certFieldTypeFilter.setFilterFieldType(filterFieldType);
            ECertFieldTableModel model = (ECertFieldTableModel) getModel();
             model.fireTableDataChanged();
            getSelectionModel().setSelectionInterval(0,0);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    class ECertFieldTypeFilter extends RowFilter
    {
        public void setFilterFieldType(ECertField.ECertFieldType filterFieldType) {
            this.filterFieldType = filterFieldType;
        }

        ECertField.ECertFieldType filterFieldType=ECertField.ECertFieldType.ALL;
        @Override
        public boolean include(Entry entry) {
            if(filterFieldType == ECertField.ECertFieldType.ALL)
            {
                return true;
            }
            else
            {
                ECertFieldTableModel model = (ECertFieldTableModel) getModel();
                Object identifier = entry.getIdentifier();
                ECertField rowElement = model.getRowElement((Integer) identifier);
                if(rowElement.getFieldType() == filterFieldType)
                {
                    return true;
                }
            }
            return false;  //To change body of implemented methods use File | Settings | File Templates.
        }
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
